<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="pyjm" label="" class="">
          <a-input v-model:value="formState.pyjm" class="w-64" placeholder="物品名称/助记码/物品编号/单号" />
        </a-form-item>
        <a-form-item name="dateRange" label="制单日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD " value-format="YYYY-MM-DD" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <BaseTable
        :data-source="tableData"
        fill
        :loading="loading"
        :scroll="{ x: 1800 }"
        column-code="000316"
        :pagination="pagination"
        @change="onPageChange"
        @expand="onShowChild"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            <!-- 0：待审核 1：待出库 2：待入库 3：已入库 -->
            <a-tag v-if="record.status === '1'" color="red">待出库</a-tag>
            <a-tag v-else-if="record.status === '2'" color="green">待入库</a-tag>
            <a-tag v-else-if="record.status === '3'" color="green">已入库</a-tag>
            <a-tag v-else color="blue">待审核</a-tag>
          </template>
          <template v-if="column.dataIndex === 'applyDh'">
            <PopoverTable :config="detailConfig" :params="{ ckbh: record.storeCode, applyDh: record.applyDh }" :width="1200">
              <template #bodyCell="{ column: popColumn, record: popRecord }">
                <template v-if="popColumn.dataIndex === 'drugName'">
                  <MedicineInfo :value="{ mc: popRecord.drugName, gg: popRecord.gg, cj: popRecord.cj }" />
                </template>
                <template v-if="popColumn.dataIndex === 'memo'">
                  <span v-if="popRecord.memo">{{ popRecord.memo }}</span>
                  <span v-else>-</span>
                </template>
              </template>
              {{ record.applyDh }}
            </PopoverTable>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <a-button type="link" size="small" :disabled="record.status !== '1'" @click="onDetail(record)">明细</a-button>
              <BaseButton confirm-text="是否确认出库?" type="link" size="small" :disabled="record.status !== '1'" @click="confirmOutStorage(record, '1')">
                确认出库
              </BaseButton>
              <a-button type="link" size="small" @click="onPrint(record)">打印</a-button>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :title="editTitle" :row-data="rowData" @success="onSuceess" />
  </div>
</template>

<script setup>
import dayjs from 'dayjs'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import BaseButton from '@/components/BaseButton'

const editTitle = ref('')
const rowData = ref({})
const router = useRouter()
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  pyjm: ''
})

const detailConfig = ref({
  url: '/oos/consume/getMtrApplyDetailList',
  columnCode: '000317',
  rowKey: 'id',
  formatResult: (data) => {
    return data.map((item, index) => ({
      ...item,
      xh: index + 1
    }))
  }
})

const { loading, tableData, getData, pagination } = useTable({
  url: '/oos/consume/getKcApplyEntityList',
  immediate: true,
  formatParams: () => {
    return {
      stdate: formState.value.dateRange?.[0],
      eddate: formState.value.dateRange?.[1],
      pyjm: formState.value.pyjm,
      status: ['1', '2', '3']
    }
  }
})

const confirmOutStorage = async (record) => {
  const res = await post('/oos/consume/applyDoMtrConfirmOutBound', { applyDh: record.applyDh })
  if (res.code !== 0) return message.error(res.msg || '确认出库失败')
  message.success(res.msg)
  getData()
}
const onDetail = (record) => {
  router.push({
    path: '/optic-warehouse/warehouse/apply/detail',
    query: {
      dh: record.applyDh
    }
  })
}

// 打印
const onPrint = async (record) => {
  const { code, msg, data } = await post('/oos/consume/printMtrApplyDetail', { applyDh: record.applyDh, ckbh: record.storeCode, mtrType: 0 })
  if (code !== 0) return message.error(msg || '获取打印数据失败')
  window.openExe?.rpt(data) //调用winform打印
}
</script>
